#include "imitateplay.h"

ImitatePlay::ImitatePlay(QWidget *parent) : QMainWindow(parent)
{
    //设置窗口
    //固定尺寸
    this->setFixedSize(mainSceneWidth,mainSceneHeight);
    //标题
    this->setWindowTitle(mainSceneName);
    //图标
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));

    //创建菜单
    QMenuBar * bar = menuBar();
    this->setMenuBar(bar);
    QMenu* startMenu = bar->addMenu("开始");
    QAction * quitAction = startMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //返回按钮
    PushButton_01 * back =new PushButton_01(":/res/BackButton.png",":/res/BackButtonSelected.png");
    back->setParent(this);
    back->move(this->width() - back->width(),this->height() - back->height());
    connect(back,&PushButton_01::clicked,[=](){
        QTimer::singleShot(500,this,[=](){
            this->hide();
            emit this->ImitateSceneBack();
        });
    });

    //初始化二维数组
    int arrayi1[4][4];
    int arrayi2[4][4];
    srand((unsigned)time(NULL));
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            arrayi1[i][j] = (rand() % 2);
            this->gameArray[i][j] = arrayi1[i][j];
            arrayi2[i][j] = (rand() % 2);
        }
    }

    for(int i = 0 ; i < 4;i++)
    {
       for(int j = 0 ; j < 4; j++)
       {

           //绘制背景图片
            QLabel* label = new QLabel;
            QPixmap pix;
            pix.load(":/res/BoardNode(1).png");
            label->setGeometry(0,0,pix.width(),pix.height());
            label->setPixmap(pix);
            label->setParent(this);
            label->move(100 + i*75,400+j*75);

            //创建金币
            QString str;
            if(this->gameArray[i][j] == 1){
                str = ":/res/Coin0001.png";
            }
            else
            {
                str = ":/res/Coin0008.png";
            }
            MyCoin * coin = new MyCoin(str);
            coin->setParent(this);
            coin->move(100 + i*75,400+j*75);
            coin->posX=i;
            coin->posY=j;
            coin->flag = gameArray[i][j];

            this->coinBtn[i][j]=coin;

            //绘制背景图片
             QLabel* label1 = new QLabel;
             QPixmap pix1;
             pix1.load(":/res/BoardNode(1).png");
             label1->setGeometry(0,0,pix1.width(),pix1.height());
             label1->setPixmap(pix1);
             label1->setParent(this);
             label1->move(535 + i*75,400+j*75);

             //创建金币
             QString str1;
             if(arrayi2[i][j] == 1){
                 str1 = ":/res/Coin0001.png";
             }
             else
             {
                 str1 = ":/res/Coin0008.png";
             }
             MyCoin * coin1 = new MyCoin(str1);
             coin1->setParent(this);
             coin1->move(535 + i*75,400+j*75);
             coin1->posX=i;
             coin1->posY=j;
             coin1->flag = gameArray[i][j];

             //点击硬币调用
             connect(coin,&MyCoin::clicked,[=](){

                coin->changeFlag();

                //同步二维数组，方便后期扩展
                gameArray[i][j]=gameArray[i][j]==0?1:0;

                if(current==0)
                {

                    //翻转周围硬币
                    QTimer::singleShot(300,this,[=](){
                        //右侧
                        if(coin->posX+1<=3)
                        {
                            this->coinBtn[coin->posX+1][coin->posY]->changeFlag();
                            gameArray[coin->posX+1][coin->posY] = gameArray[coin->posX+1][coin->posY]==0?1:0;
                        }
                        //左侧
                        if(coin->posX-1>=0)
                        {
                            this->coinBtn[coin->posX-1][coin->posY]->changeFlag();
                            gameArray[coin->posX-1][coin->posY] = gameArray[coin->posX-1][coin->posY]==0?1:0;
                        }
                        //下侧
                        if(coin->posY+1<=3)
                        {
                            this->coinBtn[coin->posX][coin->posY+1]->changeFlag();
                            gameArray[coin->posX][coin->posY+1] = gameArray[coin->posX][coin->posY+1]==0?1:0;
                        }
                        //上侧
                        if(coin->posY-1>=0)
                        {
                            this->coinBtn[coin->posX][coin->posY-1]->changeFlag();
                            gameArray[coin->posX][coin->posY-1] = gameArray[coin->posX][coin->posY-1]==0?1:0;
                        }
                    });
                }
             });
       }
    }

}
//绘图事件
void ImitatePlay::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/bfda0be809541c57e1407952d1e6042.jpg");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //标题
    pix.load(":/res/Title.png");
    painter.drawPixmap((this->width()-pix.width())*0.5,30,pix);
}
